package com.brokendust.client.api.gui;

import com.brokendust.client.PixelEngine;
import com.brokendust.client.api.module.GUIComponent;
import com.brokendust.client.api.module.button.ClickButton;
import com.brokendust.client.api.module.image.ImageShow;
import com.brokendust.client.api.module.item.ItemShow;
import com.brokendust.client.api.module.pullpanel.PullPanel;
import com.brokendust.client.api.module.text.TextLabel;
import com.brokendust.client.api.module.textfield.TextField;
import com.brokendust.client.api.util.BasePosition;
import com.brokendust.client.api.util.image.ImageControl;
import com.brokendust.client.config.gui.MenuJson;
import com.brokendust.client.config.module.button.ClickButtonJson;
import com.brokendust.client.config.module.image.ImageShowJson;
import com.brokendust.client.config.module.item.ItemShowJson;
import com.brokendust.client.config.module.pullpanel.PullPanelJson;
import com.brokendust.client.config.module.text.TextLabelJson;
import com.brokendust.client.config.module.textfield.TextFieldJson;
import com.brokendust.client.config.module.util.ActionJson;
import com.brokendust.client.config.module.util.ButtonJson;
import com.brokendust.client.config.module.util.ConversionJson;
import com.mojang.blaze3d.matrix.MatrixStack;
import net.minecraft.client.gui.IGuiEventListener;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@OnlyIn(Dist.CLIENT)
public class Menu extends Screen {
    public ResourceLocation resourceLocation;
    public String menu_name;
    public int field_230708_k_;
    public int field_230709_l_;
    public int position;
    public int x;
    public int y;
    public List<ClickButtonJson> click_button_list;
    public List<TextFieldJson> text_field_json_list;
    public List<ImageShowJson> image_json_list;
    public List<TextLabelJson> text_json_list;
    public List<ItemShowJson> itemShowJsonList;
    public List<PullPanelJson> pullPanelJsonList;
    public Map<String, GUIComponent> guiComponentMap;
    public List<MouseListener> mouseListenerList;
    int mX;
    int mY;

    public Menu(ITextComponent titleIn, MenuJson menuJson) {
        super(titleIn);
        this.menu_name = "";
        this.mX = 0;
        this.mY = 0;
        this.click_button_list = new ArrayList<ClickButtonJson>();
        this.text_field_json_list = new ArrayList<TextFieldJson>();
        this.image_json_list = new ArrayList<ImageShowJson>();
        this.text_json_list = new ArrayList<TextLabelJson>();
        this.itemShowJsonList = new ArrayList<ItemShowJson>();
        this.pullPanelJsonList = new ArrayList<PullPanelJson>();
        this.guiComponentMap = new HashMap<String, GUIComponent>();
        this.mouseListenerList = new ArrayList<MouseListener>();
        this.resourceLocation = ImageControl.getResourceLocation(menuJson.getImage());
        this.menu_name = menuJson.getMenu_name();
        this.field_230708_k_ = menuJson.getWidth();
        this.field_230709_l_ = menuJson.getHeight();
        this.position = menuJson.getPosition();
        this.x = menuJson.getX();
        this.y = menuJson.getY();
        this.addGUIComponent(menuJson);
    }

    protected void init() {
        for (ClickButtonJson clickButtonJson : this.click_button_list) {
            ClickButton clickButton = new ClickButton(clickButtonJson, this.field_230708_k_, this.field_230709_l_, 0, 0);
            clickButton.setOnPress(clickButton1 -> {
                if (!clickButtonJson.getLeftList().isEmpty()) {
                    PixelEngine.pixelConnect.send("action", ConversionJson.writeJSON(new ActionJson(clickButtonJson.getLeftList())));
                }
                PixelEngine.pixelConnect.send("button", ConversionJson.writeJSON(new ButtonJson(clickButtonJson)));
                if (clickButtonJson.getTo_menu() != null && !clickButtonJson.getTo_menu().isEmpty()) {
                    this.onClose();
                    PixelEngine.pixelConnect.send("ToMenu", clickButtonJson.getTo_menu());
                } else {
                    if (clickButtonJson.isClose()) {
                        this.onClose();
                    }
                }
            });
            this.addButton(clickButton);
            this.guiComponentMap.put(clickButtonJson.getObject_name(), clickButton);
        }
        for (TextFieldJson textFieldJson : this.text_field_json_list) {
            TextField textField = new TextField(textFieldJson, this.field_230708_k_, this.field_230709_l_, 0, 0);
            this.children.add(textField);
            this.guiComponentMap.put(textFieldJson.getObject_name(), textField);
        }
        for (ImageShowJson imageShowJson : this.image_json_list) {
            ImageShow imageShow = new ImageShow(imageShowJson, this.field_230708_k_, this.field_230709_l_, 0, 0);
            this.guiComponentMap.put(imageShowJson.getObject_name(), imageShow);
        }
        for (TextLabelJson textLabelJson : this.text_json_list) {
            TextLabel textLabel = new TextLabel(textLabelJson, this.field_230708_k_, this.field_230709_l_, 0, 0);
            this.guiComponentMap.put(textLabelJson.getObject_name(), textLabel);
        }
        for (ItemShowJson itemShowJson : this.itemShowJsonList) {
            ItemShow itemShow = new ItemShow(itemShowJson, this.field_230708_k_, this.field_230709_l_, 0, 0);
            this.guiComponentMap.put(itemShowJson.getObject_name(), itemShow);
        }
        for (PullPanelJson pullPanelJson : this.pullPanelJsonList) {
            PullPanel pullPanel = new PullPanel(pullPanelJson, this.field_230708_k_, this.field_230709_l_, this, this.position);
            this.guiComponentMap.put(pullPanelJson.getObjectID(), pullPanel);
            this.addMouseListener((MouseListener) pullPanel);
        }
        super.init();
    }

    @Override
    public void render(MatrixStack matrixStack, int mouseX, int mouseY, float particleTick) {
        this.renderMenu(matrixStack, mouseX, mouseY, particleTick);
        super.render(matrixStack, mouseX, mouseY, particleTick);
    }

    public void renderMenu(MatrixStack matrixStack, int mouseX, int mouseY, float particleTick) {
        int windowWidth = PixelEngine.minecraft.getWindow().getWidth();
        int windowHeight = PixelEngine.minecraft.getWindow().getHeight();
        PixelEngine.minecraft.getTextureManager().bind(this.resourceLocation);
        if (this.position < 10) {
            this.renderBackground(matrixStack);
            Integer[] integers = BasePosition.getMenuPoint(windowWidth, windowHeight, this.field_230708_k_, this.field_230709_l_, this.x, this.y, this.position);
            this.mX = integers[0];
            this.mY = integers[1];
            Screen.blit(matrixStack, this.mX, this.mY, 0.0f, 0.0f, this.field_230708_k_, this.field_230709_l_, this.field_230708_k_, this.field_230709_l_);
        } else {
            Screen.blit(matrixStack, this.mX, this.mY, 0.0f, 0.0f, windowWidth, windowHeight, windowWidth, windowHeight);
        }
        this.guiComponentMap.forEach((s, guiComponent) -> guiComponent.renderGUI(matrixStack, mouseX, mouseY, particleTick, this.mX, this.mY));
    }

    public void addGUIComponent(MenuJson menuJson) {
        for (String type : menuJson.getObject_list().keySet()) {
            String value = menuJson.getObject_list().get(type);
            if (type.startsWith("Button")) {
                ClickButtonJson clickButtonJson = ClickButtonJson.readJSON(value);
                clickButtonJson.refreshSize();
                this.click_button_list.add(clickButtonJson);
            } else if (type.startsWith("TextField")) {
                TextFieldJson textFieldJson = TextFieldJson.readJSON(value);
                textFieldJson.refreshSize();
                this.text_field_json_list.add(textFieldJson);
            } else if (type.startsWith("Image")) {
                ImageShowJson imageShowJson = ImageShowJson.readJSON(value);
                imageShowJson.refreshSize();
                this.image_json_list.add(imageShowJson);
            } else if (type.startsWith("Text")) {
                TextLabelJson textLabelJson = TextLabelJson.readJSON(value);
                this.text_json_list.add(textLabelJson);
            } else if (type.startsWith("Item")) {
                ItemShowJson itemShowJson = ItemShowJson.readJSON(value);
                this.itemShowJsonList.add(itemShowJson);
            } else {
                if (!type.startsWith("PullPanel")) {
                    continue;
                }
                PullPanelJson pullPanelJson = PullPanelJson.readJSON(value);
                pullPanelJson.refreshSize();
                this.pullPanelJsonList.add(pullPanelJson);
            }
        }
    }

    public void addMouseListener(MouseListener mouseListener) {
        this.mouseListenerList.add(mouseListener);
    }

    public void addButton(IGuiEventListener pixelButton) {
        this.children.add(pixelButton);
    }

}
